package com.rxmvp.baselibrary.rxretrofit.subscriber;

import com.rxmvp.baselibrary.utils.ToastUtil;
import com.jakewharton.retrofit2.adapter.rxjava2.HttpException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import io.reactivex.subscribers.DisposableSubscriber;


/**
 * 作者： huangRZ on 2017/1/21. 15:04
 * 邮箱：917647409@qq.com
 */
public abstract class BaseSubscriber<T> extends DisposableSubscriber<T> {

    @Override
    public void onComplete() {

    }

    @Override
    public void onError(Throwable e) {
        if (e instanceof UnknownHostException) {
             ToastUtil.showShortToast("请打开网络");
        } else if (e instanceof SocketTimeoutException) {
             ToastUtil.showShortToast( "请求超时");
        } else if (e instanceof ConnectException) {
             ToastUtil.showShortToast("连接失败");
        } else if (e instanceof HttpException) {
             ToastUtil.showShortToast("请求异常");
        }else {
              ToastUtil.showShortToast("其他错误");
        }
        e.printStackTrace();
        onFailure();
    }

    //在onNext执行之前执行
    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    public void onNext(T t) {
        onSuccess(t);
    }

    public void onFailure(){

    }

    public abstract void onSuccess(T response);

}
